package com.msstudio.utils;

// Decompiled by Jad v1.5.7g. Copyright 2000 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/SiliconValley/Bridge/8617/jad.html
// Decompiler options: packimports(3) fieldsfirst ansi 
import com.msstudio.BlackList;
import com.msstudio.BlackList;
import com.msstudio.midlets.MSCMIDlet;
import java.io.*;
import java.util.Vector;
import javax.microedition.lcdui.*;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreNotFoundException;

public class PkgTools {

    private static final String DBName = "MSCDB";
    private static final int C_Record_Num = 13;
    private static final int C_BlackListStart = 3;
    private static String DBStrings[] = new String[13];

    public PkgTools() {
    }

    public static String To2Str(int i) {
        if (i < 10) {
            return "0" + i;
        } else {
            return "" + i;
        }
    }

    public static Image createImage(String s) {
        Image image = null;
        try {
            image = Image.createImage(s);
        } catch (IOException ioexception) {
        }
        return image;
    }

    public static void EmptyForm(Form form) {
        for (int i = form.size() - 1; i >= 0; i--) {
            form.delete(i);
        }

    }

    public static void EmptyList(List list) {
        for (int i = list.size() - 1; i >= 0; i--) {
            list.delete(i);
        }

    }

    public static String Encodecgi(String s) {//���뺯���ַ��е�' '��'&'��','��'\r'��'\t'��'\n'��'%'��'='
        //��Щ�ַ�ת��������%XX����ʽ������XXΪ�����ַ��ASCII���ʮ�������ʽ��
        StringBuffer stringbuffer = new StringBuffer();
        if (s == null) {
            return "";
        }
        char ac[] = s.toCharArray();
        int i = ac.length;
        if (i == 0) {
            return "";
        }
        for (int j = 0; j < i; j++) {
            if (ac[j] == ' ' || ac[j] == '&' || ac[j] == ',' || ac[j] == '\r' || ac[j] == '\t' || ac[j] == '\n' || ac[j] == '%' || ac[j] == '=') {
                String s1 = Integer.toHexString(ac[j]);
                if (s1.length() == 1) {
                    stringbuffer.append("%0" + s1);
                } else {
                    stringbuffer.append("%" + s1);
                }
            } else {
                stringbuffer.append(ac[j]);
            }
        }

        return stringbuffer.toString();
    }

    public static String Decodecgi(String s) {//���뺯���������%XX��ʮ�������ݵ��ַ��е�%XXת����һ���ַ�
        //����ת������ַ�
        StringBuffer stringbuffer = new StringBuffer();
        if (s == null) {
            return "";
        }
        char ac[] = s.toCharArray();
        int j = ac.length;
        if (j == 0) {
            return "";
        }
        for (int i = 0; i < j;) {
            if (ac[i] == '%') {
                String s1 = String.valueOf(ac, i + 1, 2);
                char c;
                try {
                    c = (char) Integer.parseInt(s1, 16);
                } catch (Exception exception) {
                    c = ' ';
                }
                stringbuffer.append(c);
                i += 3;
            } else {
                stringbuffer.append(ac[i]);
                i++;
            }
        }

        return stringbuffer.toString();
    }

    public static String GetParaVal(String s, String s1) {
        String s2 = null;
        int i = 0;
        int j = 0;
        int k = 0;
        int l = 0;
        if (s == null || s1 == null) {
            return "";
        }
        s = s + "=";
        k = s.length();
        l = s1.length();
        if (l == 0) {
            return "";
        }
        if (k == 0 || k > 9) {
            return "";
        }
        i = s1.indexOf(s);
        if (i == -1) {
            return "";
        }
        j = s1.indexOf(38, i);
        if (j == -1) {
            s2 = s1.substring(i + k);
        } else {
            s2 = s1.substring(i + k, j);
        }
        return s2;
    }

    public static int GetMultiPara(String as[], String s) {
        int i = 0;
        String s1 = null;
        int j = 0;
        int k = 0;
        boolean flag = false;
        if (as == null || s == null) {
            return -1;
        }
        j = s.length();
        if (j >= 2560) {
            return -1;
        }
        i = s.indexOf(44);
        if (i == -1) {
            as[0] = s;
            return 1;
        }
        as[0] = s.substring(0, i);
        if (++k == as.length) {
            return k;
        }
        for (s1 = s.substring(i + 1); (i = s1.indexOf(44)) != -1; s1 = s1.substring(i + 1)) {
            as[k] = s1.substring(0, i);
            if (++k == as.length) {
                return k;
            }
        }

        as[k] = s1;
        return ++k;
    }

    public static byte[] Unicode2Byte(String s) {
        int i = s.length();
        byte abyte0[] = new byte[i << 1];
        int j = 0;
        for (int k = 0; k < i; k++) {
            char c = s.charAt(k);
            abyte0[j++] = (byte) (c & 0xff);
            abyte0[j++] = (byte) (c >> 8);
        }

        return abyte0;
    }

    public static byte[] convertUnicode2UTF8Byte(String s) {
        int i = s.length();
        byte abyte0[] = new byte[i << 2];
        int j = 0;
        for (int k = 0; k < i; k++) {
            char c = s.charAt(k);
            if (c < '\200') {
                abyte0[j++] = (byte) c;
            } else if (c < '\u0800') {
                abyte0[j++] = (byte) (c >> 6 & 0x1f | 0xc0);
                abyte0[j++] = (byte) (c & 0x3f | 0x80);
            } else if (c < '\0') {
                abyte0[j++] = (byte) (c >> 12 & 0xf | 0xe0);
                abyte0[j++] = (byte) (c >> 6 & 0x3f | 0x80);
                abyte0[j++] = (byte) (c & 0x3f | 0x80);
            } else if (c < '\0') {
                abyte0[j++] = (byte) (c >> 18 & 0x7 | 0xf0);
                abyte0[j++] = (byte) (c >> 12 & 0x3f | 0x80);
                abyte0[j++] = (byte) (c >> 6 & 0x3f | 0x80);
                abyte0[j++] = (byte) (c & 0x3f | 0x80);
            }
        }

        byte abyte1[] = new byte[j];
        for (int l = 0; l < j; l++) {
            abyte1[l] = abyte0[l];
        }

        return abyte1;
    }

    public static void saveDB() {
        RecordStore recordstore = null;
        ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
        DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
        try {
            recordstore = RecordStore.openRecordStore(DBName, true);
            int i = recordstore.getNumRecords();
            for (int j = 0; j < 13; j++) {
                dataoutputstream.writeUTF(DBStrings[j]);
            }

            byte abyte0[] = bytearrayoutputstream.toByteArray();
            if (i == 0) {
                recordstore.addRecord(abyte0, 0, abyte0.length);
            } else {
                recordstore.setRecord(1, abyte0, 0, abyte0.length);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                dataoutputstream.close();
            } catch (Exception ex) {
            }
            if (recordstore != null) {
                try {
                    recordstore.closeRecordStore();
                } catch (Exception ex) {
                }
            }
        }
    }

    public static void loadDB() {
        RecordStore recordstore = null;
        DataInputStream datainputstream = null;
        try {
            recordstore = RecordStore.openRecordStore(DBName, true);
            int k = recordstore.getNumRecords();
            if (k == 0) {
                DBStrings[0] = "0";
                DBStrings[1] = "";
                DBStrings[2] = "1";
                for (int i = 0; i < 10; i++) {
                    DBStrings[i + 3] = "0";
                }

            } else {
                byte abyte0[] = recordstore.getRecord(1);
                ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
                datainputstream = new DataInputStream(bytearrayinputstream);
                for (int j = 0; j < 13; j++) {
                    DBStrings[j] = datainputstream.readUTF();
                }

            }
        } catch (RecordStoreNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                datainputstream.close();
            } catch (Exception ex) { }
            if (recordstore != null) {
                try {
                    recordstore.closeRecordStore();
                } catch (Exception e) {
                }
            }
        }
    }

    public static void setUserInfo(String s, String s1) {
        DBStrings[0] = s;
        DBStrings[1] = s1;
        saveDB();
    }

    public static void setOtherInfo(String s, String s1, BlackList blacklist) {
        if (s1 != null) {
            DBStrings[2] = s1;
        }
        if (blacklist != null) {
            for (int i = 0; i < 10; i++) {
                DBStrings[i + 3] = blacklist.getDBItem(i);
            }

        }
        saveDB();
    }

    private static void fillVectorWithDB(String s, Vector vector, int i, int j) {
        if (DBStrings[i] == null) {
            return;
        }
        for (int k = i; k < i + j; k++) {
            if (DBStrings[k].length() > 4) {
                vector.addElement(DBStrings[k].trim());
            }
        }

    }

    public static String getUIN() {
        if (DBStrings[0] == null) {
            return "0";
        } else {
            return DBStrings[0].trim();
        }
    }

    public static String getPassWord() {
        if (DBStrings[1] == null) {
            return "";
        } else {
            return DBStrings[1];
        }
    }

    public static String getControl() {
        if (DBStrings[2] == null) {
            return "1";
        } else {
            return DBStrings[2].trim();
        }
    }

    public static void getBlackList(BlackList blacklist) {
        fillVectorWithDB(DBName, blacklist.vList, 3, 10);
    }

    public static void loadFriendData() {
        label0:
        {
            RecordStore recordstore = null;
            Object obj = null;
            DataInputStream datainputstream = null;
            Object obj1 = null;
            try {
                recordstore = RecordStore.openRecordStore(DBName, true);
                int i = recordstore.getNumRecords();
                if (i >= 2) {
                    byte abyte0[] = recordstore.getRecord(2);
                    ByteArrayInputStream bytearrayinputstream = new ByteArrayInputStream(abyte0);
                    datainputstream = new DataInputStream(bytearrayinputstream);
                    String s = datainputstream.readUTF();
                    if (s.equals(MSCMIDlet.httpPoster.GetSelfUIN())) {
                        for (long l = datainputstream.readLong(); l != 0L; l = datainputstream.readLong()) {
                            String s1 = datainputstream.readUTF();
                            short word0 = datainputstream.readShort();
                            int j = MSCMIDlet.friendRecord.getUserLocation(l);
                            if (j >= 0) {
                                MSCMIDlet.friendRecord.SetInforByIndex(j, true, s1, word0);
                            }
                        }

                    }
                }
            } catch (RecordStoreNotFoundException recordstorenotfoundexception) {
                if (datainputstream != null) {
                    try {
                        datainputstream.close();
                    } catch (Exception exception2) {
                    }
                }
                if (recordstore != null) {
                    try {
                        recordstore.closeRecordStore();
                    } catch (Exception exception3) {
                    }
                }
                break label0;
            } catch (Exception exception4) {
                if (datainputstream != null) {
                    try {
                        datainputstream.close();
                    } catch (Exception exception5) {
                    }
                }
                if (recordstore != null) {
                    try {
                        recordstore.closeRecordStore();
                    } catch (Exception exception6) {
                    }
                }
                break label0;
            } finally {
                if (datainputstream != null) {
                    try {
                        datainputstream.close();
                    } catch (Exception exception8) {
                    }
                }
                if (recordstore != null) {
                    try {
                        recordstore.closeRecordStore();
                    } catch (Exception exception9) {
                    }
                }
            }
            if (datainputstream != null) {
                try {
                    datainputstream.close();
                } catch (Exception exception) {
                }
            }
            if (recordstore != null) {
                try {
                    recordstore.closeRecordStore();
                } catch (Exception exception1) {
                }
            }
            break label0;
        }
    }

    public static void saveFriendData(Vector vector) {
        label0:
        {
            RecordStore recordstore = null;
            ByteArrayOutputStream bytearrayoutputstream = new ByteArrayOutputStream();
            DataOutputStream dataoutputstream = new DataOutputStream(bytearrayoutputstream);
            Object obj = null;
            try {
                recordstore = RecordStore.openRecordStore(DBName, true);
                int i = recordstore.getNumRecords();
                dataoutputstream.writeUTF(MSCMIDlet.httpPoster.GetSelfUIN());
                for (int j = 0; j < vector.size(); j++) {
                    if (((FriendRecordItem) vector.elementAt(j)).sState != 0) {
                        dataoutputstream.writeLong(((FriendRecordItem) vector.elementAt(j)).lUserID);
                        dataoutputstream.writeUTF(((FriendRecordItem) vector.elementAt(j)).strName);
                        dataoutputstream.writeShort(((FriendRecordItem) vector.elementAt(j)).sFaceID);
                    }
                }

                dataoutputstream.writeLong(0L);
                byte abyte0[] = bytearrayoutputstream.toByteArray();
                if (i < 2) {
                    recordstore.addRecord(abyte0, 0, abyte0.length);
                } else {
                    recordstore.setRecord(2, abyte0, 0, abyte0.length);
                }
            } catch (Exception exception2) {
                try {
                    dataoutputstream.close();
                } catch (Exception exception3) {
                }
                if (recordstore != null) {
                    try {
                        recordstore.closeRecordStore();
                    } catch (Exception exception4) {
                    }
                }
                break label0;
            } finally {
                try {
                    dataoutputstream.close();
                } catch (Exception exception6) {
                }
                if (recordstore != null) {
                    try {
                        recordstore.closeRecordStore();
                    } catch (Exception exception7) {
                    }
                }
            }
            try {
                dataoutputstream.close();
            } catch (Exception exception) {
            }
            if (recordstore != null) {
                try {
                    recordstore.closeRecordStore();
                } catch (Exception exception1) {
                }
            }
            break label0;
        }
    }
}
